package org.ceteca.explica.core.util;


import java.awt.geom.Point2D;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.ceteca.explica.core.InterfaceExplica;


/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class Converter {
	private static Converter instance = null;

	public Converter() {

	}

	public static Converter getInstance() {
		if (instance == null)
			instance = new Converter();

		return instance;
	}

	public float parseFloat(String s) {
		float result = 0;

		NumberFormat form = NumberFormat.getNumberInstance();
		try {
			result = form.parse(s).floatValue();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}
	public java.lang.Double parseDouble(String s) {
		java.lang.Double result = 0.0;

		NumberFormat form = NumberFormat.getNumberInstance();
		try {
			result = form.parse(s).doubleValue();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}
	public BigDecimal parseBigDecimal(String s) {
		BigDecimal result = new BigDecimal(0);
		NumberFormat form = NumberFormat.getNumberInstance();
		try {
			result = new BigDecimal(form.parse(s).doubleValue());
		} catch (ParseException e) {
			//e.printStackTrace();
			return new BigDecimal(0);
		}
		return result;
	}

	public Long parseLong(String s) {
		Long result = new Long(0);
		NumberFormat form = NumberFormat.getNumberInstance();
		try {
			result = form.parse(s).longValue();
		} catch (ParseException e) {
			//e.printStackTrace();
			return new Long(0);
		}
		return result;
	}

	
	
	public Integer parseInteger(String s) {
		Integer result = new Integer(0);
		NumberFormat form = NumberFormat.getNumberInstance();
		try {
			result = form.parse(s).intValue();
		} catch (ParseException e) {
			//e.printStackTrace();
			return new Integer(0);
		}
		return result;
	}	
	public String toString(float f) {

		NumberFormat formato = NumberFormat.getNumberInstance();

		return formato.format(f);
	}

	public String toString(Integer i){
		if (i==null)
			return "0";
		else{
			NumberFormat formato = NumberFormat.getIntegerInstance();
			return formato.format(i);
		}
		
	}
	
	
	public String toString(long l) {
		NumberFormat formato = NumberFormat.getIntegerInstance();
		return formato.format(l);
	}

	public String toString(java.lang.Double d){
		if (d==null)
			return "";
		else
			return toString(d.doubleValue());
	}
	
	public String toString(double f) {
			
		NumberFormat formato = NumberFormat.getNumberInstance();

		return formato.format(f);
	}

	public String toString(Date d) {
		try{
		SimpleDateFormat formato = new SimpleDateFormat(
				InterfaceExplica.CONST_SHORT_DATE_PATTERN);

		return formato.format(d);
		}catch(Exception e){
			return "";
		}
	}

	public String toString(BigDecimal price) {

		if (price == null)
			return "0";
		else {
			NumberFormat formato = NumberFormat.getNumberInstance();
			return formato.format(price);
		}
	}

	public Date toDate(org.eclipse.swt.widgets.DateTime c) {
		Calendar cal = Calendar.getInstance();
		if (c != null) {
			cal.set(c.getYear(), c.getMonth(), c.getDay());
			return cal.getTime();
		} else
			return null;

	}

	public int day(Date d) {
		if (d == null)
			return 0;
		Calendar cal = Calendar.getInstance();

		cal.setTime(d);
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	public int month(Date d) {
		if (d == null)
			return 0;
		Calendar cal = Calendar.getInstance();

		cal.setTime(d);
		return cal.get(Calendar.MONTH);
	}

	public int year(Date d) {
		if (d == null)
			return 0;
		Calendar cal = Calendar.getInstance();

		cal.setTime(d);
		return cal.get(Calendar.YEAR);
	}

	public void setDate(org.eclipse.swt.widgets.DateTime widget, Date d) {
		if (d != null) {
			widget.setDay(Converter.getInstance().day(d));
			widget.setMonth(Converter.getInstance().month(d));
			widget.setYear(Converter.getInstance().year(d));
		} else {
			widget.setDay(20);
			widget.setMonth(8);
			widget.setYear(1969);
		}
	}
	

	/**Calcula la letra del nif según legislacion española
	 * @param dni
	 * @return
	 */
	public char letraNIF(String dni){
		if (!Validator.getInstance().isValidDNI(dni))
			return 0;
		else{
			String cadena = "TRWAGMYFPDXBNJZSQVHLCKET";
			Integer dnii = new Integer(dni);
			int dniint = dnii.intValue();
			int posicion = dniint % 23;
			char letra = cadena.charAt(posicion);
			return letra;
		}
		
			
	}
	
	public Point2D parseCoordinates(String s){
		try{
		String[] coordenadas=s.split(",");
		java.lang.Double longitude=new java.lang.Double(coordenadas[0]);
		java.lang.Double latitude= new java.lang.Double(coordenadas[1]);
		
		Point2D result= new Point2D.Double(latitude, longitude);
		


		return result;
		}catch(Exception e){
			return new Point2D.Double(51.477222,0);
		}
	}
	
	
	public String toString(String string){
		return string==null?"":string;
	}

	public Date parseDate(String text) {
		Date result;
		try{
		SimpleDateFormat formato = new SimpleDateFormat(
				InterfaceExplica.CONST_SHORT_DATE_PATTERN);
		result= formato.parse(text);
		return result;
		}catch(Exception e){
			return null;
		}
	}

	
}
